import isObject from "lodash/isObject";
import cloneDeep from "lodash/cloneDeep";
import forEach from "lodash/forEach";

export function defineFormRules(rules) {
    var res = {};
    var defaultrule = {
        validate: {
            presence: true,
        },
        attr: {
            value: "",
        }
    };
    forEach(rules, function (rule, rulekey) {
        if (isObject(rule)) {
            res[rulekey] = Object.assign({}, cloneDeep(defaultrule), cloneDeep(rule));
        } else {
            res[rulekey] = cloneDeep(defaultrule);
        }
    });
    return res;
};

export function resetForm(form) {
    var formFields = form.getFormFields();
    if (!formFields) {
        throw new Error("need formFields");
    }
    forEach(form.data, function (data, datakey) {
        var dfv = "";
        if (formFields[datakey] && formFields[datakey].attr && formFields[datakey].attr.value) {
            dfv = formFields[datakey].attr.value;
        }
        data.value = dfv;
    })
}